﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MethodWorx.CMS.Plugins;
using MethodWorx.CMS.Services;
using System.Web.Mvc;
using MethodWorx.CMS.Content;
using MethodWorx.CMS.UI.Application.ActionResults;

namespace MethodWorx.CMS.Workflow.PublishContentItem
{
    public class PublishContentItemConfigurationController : Controller
    {
        private IdeExtension view;
        private WorkflowService service;

        public PublishContentItemConfigurationController(IdeExtension view, WorkflowService service)
        {
            this.view = view;
            this.service = service;
        }

        [HttpGet]
        public ActionResult Invoke(Page instance, string workflow, Guid? stepId, Guid? parentStepId)
        {
            var config = new PublishContentItemConfiguration();
            var step = Step(instance, workflow, stepId, parentStepId, config);
            if(step.ComponentConfiguration == null)
                step.ComponentConfiguration = config;
            if(step == null)
            {
                this.ViewData["error"] = "Step does not exist, or configuration does not exist";
                return View("SimpleError");
            }
            return new EmbeddedViewResult(view.WebEditor, step.ComponentConfiguration);
        }

        [HttpPost]
        public ActionResult Invoke(Page instance, string workflow, string submit, Guid? stepId, Guid? parentStepId)
        {
            PublishContentItemConfiguration config = new PublishContentItemConfiguration();
            this.TryUpdateModel<PublishContentItemConfiguration>(config);

            var model = Step(instance, workflow, stepId, parentStepId, config);
            model.ComponentConfiguration = config;
            

            if ((submit ?? "").ToLower() == "cancel")
                return new CloseDialogResult();

            if (ModelState.IsValid)
            {
                //
                //  ok, everything is ok, we need to save the workflow
                this.service.SaveWorkflow(instance, workflow, stepId, parentStepId, model);
                return new CloseDialogResult("saved");
            }

            return new EmbeddedViewResult(view.WebEditor, model.ComponentConfiguration);
        }

        private WorkflowConfigurationStep Step(Page instance, string workflowName, Guid? stepId, Guid? parentStepId, PublishContentItemConfiguration config)
        {
            WorkflowConfigurationStep step = null;
            var workflow = instance.GetWorkflowConfiguration(true).FirstOrDefault(w => w.Name.ToLower() == workflowName.ToLower());
            if (workflow != null)
            {
                if (stepId == null)
                {
                    Guid id = Guid.NewGuid();
                    Guid branchId = Guid.NewGuid();

                    step = new WorkflowConfigurationStep()
                               {
                                   Id = id,
                                   ComponentId = PublishContentItemWorkflowComponent.ComponentId,
                                   ComponentConfiguration = config,
                               };
                }
                else
                {
                    step = workflow.FindStep(stepId.Value);
                }
            }
            
            return step;
        }
    }
}
